package org.ims.inv.service.system.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;
import org.ims.inv.dao.system.DAOAppRole;
import org.ims.inv.model.system.ModelAppRole;
import org.ims.inv.service.system.ServiceAppRole;

import cn.trymore.core.exception.DAOException;
import cn.trymore.core.exception.ServiceException;
import cn.trymore.core.service.impl.ServiceGenericImpl;
import cn.trymore.core.util.UtilString;

/**
 * Service: 系统角色
 * 
 * @author Jeccy.Zhao
 *
 */
public class ServiceAppRoleImpl
extends ServiceGenericImpl<ModelAppRole> implements ServiceAppRole
{
	/**
	 * The repository of application role.
	 */
	private DAOAppRole daoAppRole;
	
	public ServiceAppRoleImpl(DAOAppRole dao)
	{
		super(dao);
		
		this.setDaoAppRole(dao);
	}
	
	/*
	 * (non-Javadoc)
	 * @see org.shengrui.oa.service.system.ServiceAppRole#getRoleByName(java.lang.String)
	 */
	@Override
	public ModelAppRole getRoleByName(String roleName) throws ServiceException
	{
		try
		{
			return this.daoAppRole.getRoleByName(roleName);
		} 
		catch (Exception e)
		{
			throw new ServiceException(e);
		}
	}
	
	/*
	 * (non-Javadoc)
	 * @see org.ims.inv.service.system.ServiceAppRole#getAllRoles()
	 */
	public List<ModelAppRole> getAllRoles () throws ServiceException
	{
		try
		{
			return this.daoAppRole.getListByCriteria(getCriterias(null));
		} 
		catch (DAOException e)
		{
			throw new ServiceException(e);
		}
	}
	
	/*
	 * (non-Javadoc)
	 * @see cn.trymore.core.service.impl.ServiceGenericImpl#getCriterias(cn.trymore.core.model.ModelBase)
	 */
	protected DetachedCriteria getCriterias(ModelAppRole entity)
	{
		DetachedCriteria criteria = DetachedCriteria.forClass(ModelAppRole.class);
		
		if (entity != null)
		{
			if (UtilString.isNotEmpty(entity.getRoleKey()))
			{
				criteria.add(Restrictions.like("roleKey", entity.getRoleKey(), MatchMode.ANYWHERE));
			}
			
			if (UtilString.isNotEmpty(entity.getRoleName()))
			{
				criteria.add(Restrictions.like("roleName", entity.getRoleName(), MatchMode.ANYWHERE));
			}
			
			if (UtilString.isNotEmpty(entity.getRoleDesc()))
			{
				criteria.add(Restrictions.like("roleDesc", entity.getRoleDesc(), MatchMode.ANYWHERE));
			}
		}
		
		return criteria;
	}
	
	/*
	 * (non-Javadoc)
	 * @see org.ims.inv.service.system.ServiceAppRole#getSecurityDataSource()
	 */
	@Override
	public Map<String, Set<String>> getSecurityDataSource()
			throws ServiceException
	{
		Map<String, Set<String>> securityRoleData = new HashMap<String, Set<String>>();
		return securityRoleData;
	}
	
	public void setDaoAppRole(DAOAppRole daoAppRole)
	{
		this.daoAppRole = daoAppRole;
	}

	public DAOAppRole getDaoAppRole()
	{
		return daoAppRole;
	}
	
}
